/*
package com.htht.job.executor.hander.shardinghandler;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.htht.job.core.handler.SharingHandler;
import com.htht.job.core.util.ResultUtil;
import com.htht.job.executor.model.downupload.DownResult;
import com.htht.job.executor.model.ftp.Ftp;
import com.htht.job.executor.model.paramtemplate.DownParam;
import com.htht.job.executor.service.downupload.DownResultService;
import com.htht.job.executor.service.ftp.FtpService;
import com.htht.job.executor.util.downLoad.FileUtil;
import com.htht.job.executor.util.downLoad.FtpUtil;

import org.apache.commons.net.ftp.FTPFile;
import org.htht.util.ApacheFtpUtil;
import org.htht.util.DataTimeHelper;
import org.htht.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("downJobHandlerShard")
public class DownJobHandlerShard implements SharingHandler {
    @Autowired
    private DownResultService downResultService;
    @Autowired
    private FtpService ftpService;
    private Date beginTime;
    private Date endTime;

    @Override
    public ResultUtil<List<String>> execute(String params, Map fixmap, Map dymap) throws Exception {
        ResultUtil<List<String>> result = new ResultUtil<List<String>>();
        List<Map> mapList = new ArrayList<Map>();
        DownParam downParam = JSON.parseObject(params, DownParam.class);
        */
/***======1.获取开始和结束日期==========***//*

        this.getBeginAndEndTime(downParam,result);
        if (!result.isSuccess()) {
            return result;
        }
        */
/***======2.获取该时间段内数据库中的数据==========***//*

        List<String> existFileList = downResultService.findFilesByTime(beginTime, endTime);
        
        */
/***======3.获取文件符合条件的文件list==========***//*

        List<String> files = this.getFileList(downParam,beginTime, endTime, existFileList,result);
        if (!result.isSuccess()) {
            return result;
        }

        result.setResult(files);
        return result;
    }

    private void getBeginAndEndTime(DownParam downParam, ResultUtil<List<String>> result) {
    	try {
    		Calendar calendar = Calendar.getInstance();
    		if ("now".equals(downParam.getDownloadType())) {
    			int days =Integer.parseInt(downParam.getDownloadDays());
    			endTime = calendar.getTime();
    			calendar.set(Calendar.DAY_OF_YEAR, -days);
    			beginTime = calendar.getTime();
    		} else if ("history".equals(downParam.getDownloadType())) {
//    			2018-02-09 00:00:00;2018-02-09 23:59:59
    			String[] temp = downParam.getDownloadDate().split(" - ");
    			beginTime = org.htht.util.DateUtil.getDate(temp[0], "yyyy-MM-dd HH:mm:ss");
    			endTime = org.htht.util.DateUtil.getDate(temp[1], "yyyy-MM-dd HH:mm:ss");
    		}
    	} catch (Exception e) {
    		result.setErrorMessage("数据开始时间和结束时间获取失败");
    		throw new RuntimeException();
    	}
    }

    public List<String> getFileList(DownParam downParam,Date beginTime,Date endTime,List<String> existFileList,ResultUtil<List<String>> result) {
    	
    	//包括："原路径＋原名称＋","＋新路径＋新名称＋","+文件大小"
    	List<String> fileList = new ArrayList<String>();
    	
    	
    	//把遍历的目录保存到pathList中 确保目录只遍历一次
    	List<String> pathList = this.getPathList(downParam.getForPath(),beginTime,endTime);
    	for(String forPath:pathList){
    		try {
        		if ("ftp".equals(downParam.getForSouceType())) {
        			Ftp ftp = ftpService.getById(downParam.getForFtp());
//        			FtpUtil ftpUtil = new FtpUtil();
        			ApacheFtpUtil ftpUtil = new ApacheFtpUtil(ftp);
        			if(ftpUtil.connectServer()){
        				FTPFile[] list = ftpUtil.getDataFileList(forPath, downParam.getDownFileNamePattern());
        				ftpUtil.closeServer();
        				for (FTPFile file : list) {
        					//文件名中的时间
            				Date fileDate = new Date(DataTimeHelper.getDataTimeFromFileNameByPattern(file.getName(), downParam.getDataTimePattern()));
            				//判断该时间是否在开始和结束时间之间
            				if(fileDate.after(endTime) || fileDate.before(beginTime)){
            					continue;
            				}
            				//重命名
            				String downloadFileName = DateUtil.getPathByDate(downParam.getDownloadFileName(),fileDate);
            				//
            				if(existFileList.contains(forPath+"#"+downloadFileName)){
            					continue;
            				}
            				//目标路径 用文件的时间替换目标文件的通配符
            				String toPath =DateUtil.getPathByDate(downParam.getToPath(),fileDate).replaceAll("\\\\", "/");
            				if(!toPath.endsWith("/")){
            					toPath+="/";
            				}
            				//源路径 用文件的时间替换目标文件的通配符
            				forPath =forPath.replaceAll("\\\\", "/");
            				if(!forPath.endsWith("/")){
            					forPath+="/";
            				}
            				//包括："原路径＋原名称＋","＋新路径＋新名称＋","+文件大小"
            				fileList.add(forPath+file.getName()+","+toPath+downloadFileName+","+file.getSize()+","+fileDate.getTime());
        				}
        			}
        			
        		} else if ("file".equals(downParam.getForSouceType())) {
        			FileUtil fileUtil = new FileUtil();
        			//得到符合文件名正则的文件
        			List<File> files = org.htht.util.FileUtil.iteratorFile(new File(forPath), downParam.getDownFileNamePattern());
        			
        			for (File file : files) {
        				//文件名中的时间
        				Date fileDate = new Date(DataTimeHelper.getDataTimeFromFileNameByPattern(file.getName(), downParam.getDataTimePattern()));
        				//判断该时间是否在开始和结束时间之间
        				if(fileDate.after(endTime) || fileDate.before(beginTime)){
        					continue;
        				}
        				//重命名
        				String downloadFileName = DateUtil.getPathByDate(downParam.getDownloadFileName(),fileDate);
        				//目标路径 
        				String toPath =DateUtil.getPathByDate(downParam.getToPath(),fileDate);
        				//判断是否已经下载了
        				if(existFileList.contains(toPath+"#"+downloadFileName)){
        					continue;
        				}
        				//目标路径 用文件的时间替换目标文件的通配符
        				toPath.replaceAll("\\\\", "/");
        				if(!toPath.endsWith("/")){
        					toPath+="/";
        				}
        				//包括："原路径＋原名称＋","＋新路径＋新名称＋","+文件大小"
        				fileList.add(file.getAbsolutePath()+","+toPath+downloadFileName+","+file.length()+","+fileDate.getTime());
        			}
        		}
        	} catch (Exception e) {
        		throw new RuntimeException();
        	}    		
    		
    	}
    	return fileList;
    }

    private List<String> getPathList(String formPath, Date beginTime,Date endTime) {
    	List<String> pathList = new ArrayList<String>();
    	if(formPath.contains("{")){
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTime(beginTime);
//    		calendar.add(Calendar.HOUR, -8);//转换成标准时间
    		while(calendar.getTime().before(endTime)){
    			String forPath =DateUtil.getPathByDate(formPath,calendar.getTime());
    			if(pathList.contains(forPath)){
    				continue;
    			}
    			pathList.add(forPath);
    			calendar.add(Calendar.DAY_OF_YEAR, 1);
    		}
    	}else{
    		pathList.add(formPath);
    	}
		return pathList;
	}

}
*/
